package day03;

import java.util.Stack;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/3/13 14:54
 * 包含min函数的栈
 */
public class Offer30 {
    /** initialize your data structure here. */
    public Offer30() {

    }

    Stack<Integer> stack01 = new Stack<>();
    Stack<Integer> stack02 = new Stack<>();

    public void push(int x) {
        stack01.push(x);
        if (stack02.isEmpty() || stack02.peek() >= x) {
            stack02.push(x);
        }
    }

    public void pop() {
        if (!stack01.isEmpty()) {
            int val = stack01.pop();
            if (!stack02.isEmpty() && stack02.peek() == val) {
                stack02.pop();
            }
        }
    }

    public int top() {
        return stack01.peek();
    }

    public int min() {
        return stack02.peek();
    }
}
